﻿package gmapAssets
{
	import com.google.maps.overlays.Marker;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	import model.GlobalTextFormat;
	
	public class InfoWindow extends Sprite
	{		
		public var bg:InfoWindowBg;
		public var goButton:GoMovieClip;
		private var titleLabel:TextField;
		private var descLabel:TextField;
		private var downArrow:DownArrow;
		public var id:String;
		public var data:Object;
		private var max:Number;
		private var xOffSet:Number;
		private var yOffSet:Number;
		private var downArrowXOffSet:Number;
		
		public function InfoWindow()
		{
			bg = new InfoWindowBg();
			downArrow = new DownArrow();
			goButton = new GoMovieClip();
			titleLabel = new TextField();
			descLabel = new TextField();
			
			titleLabel.mouseEnabled = false;
			descLabel.mouseEnabled = false;
			
			addChild(bg);
			addChild(downArrow);
			addChild(goButton);		
			
			xOffSet = -135;
			yOffSet = -103;
			downArrowXOffSet = 120;
			
			downArrow.x = downArrowXOffSet;
			downArrow.y = bg.height;
			
			goButton.addEventListener(MouseEvent.CLICK, goButtonHandler);
			bg.addEventListener(MouseEvent.CLICK, bgHandler);
		}
		
		private function goButtonHandler(event:MouseEvent):void
		{
			this.dispatchEvent(new Event("GOBUTTON_SELECTED",true));
		}
		
		private function bgHandler(event:MouseEvent):void
		{
			bg.dispatchEvent(new Event("WINDOW_SELECTED"));
		}
		
		public function set title(value:String):void
		{
			if(!contains(titleLabel))
			{				
				titleLabel.selectable = false;
				titleLabel.multiline = true; 
				titleLabel.autoSize = TextFieldAutoSize.LEFT;
				addChild(titleLabel);
			}			
			
			titleLabel.text = value;	
			titleLabel.x = 5;
			titleLabel.y = 2;
			titleLabel.setTextFormat(GlobalTextFormat.infoTitleStyle);
		}
		
		public function set description(value:String):void
		{
			if(!contains(descLabel))
			{				
				descLabel.selectable = false;
				descLabel.multiline = true; 
				descLabel.autoSize = TextFieldAutoSize.LEFT;
				addChild(descLabel);
			}			
			
			descLabel.text = value;	
			descLabel.x = 5;
			descLabel.y = titleLabel.textHeight + 2;
			descLabel.setTextFormat(GlobalTextFormat.infoDescStyle);
			
			if(descLabel.textWidth > titleLabel.textWidth)
			{
				bg.width = descLabel.textWidth + 45;
			}else
			{
				bg.width = titleLabel.textWidth + 45;
			}
			
			goButton.x = bg.width - goButton.width - 5;
			goButton.y = bg.height - goButton.height - 5;
			
			setDefaultOffSet();
		}
		
		private function setDefaultOffSet():void
		{
			if(bg.width < 170)
			{
				xOffSet = -135 + 60;
				downArrow.x = downArrowXOffSet - 60;
			}
			
		}		
		
		public function getOffsetX():Number
		{
			return xOffSet;
		}
		
		public function getOffsetY():Number
		{
			return yOffSet;
		}
	}
}